SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO
/*SELECT idSubOrganizacion AS ID, Descripcion+'  - ('+dbo.tb_Localidades.Localidad +')' AS Descripcion, Org, EsPropia, Nivel, Modalidad, Zona, dbo.tb_SubOrganizaciones.Localidad, Categoria
FROM  dbo.tb_SubOrganizaciones
LEFT Outer JOIN dbo.tb_Localidades  ON dbo.tb_Localidades.IdLocalidad = dbo.tb_SubOrganizaciones.Localidad
//  VERSION CAMBIADA 29-04-2008
SELECT     TOP 100 PERCENT dbo.tb_SubOrganizaciones.idSubOrganizacion AS ID, dbo.tb_SubOrganizaciones.Descripcion + ' (' + CONVERT(varchar, 
                      dbo.tb_Organizaciones.CUISE) + ')' + '  -' + dbo.tb_Localidades.localidad AS Descripcion, dbo.tb_SubOrganizaciones.Org, 
                      dbo.tb_SubOrganizaciones.EsPropia, dbo.tb_SubOrganizaciones.Nivel, dbo.tb_SubOrganizaciones.Modalidad, dbo.tb_SubOrganizaciones.Zona, 
                      dbo.tb_SubOrganizaciones.Localidad, dbo.tb_SubOrganizaciones.Categoria
FROM         dbo.tb_SubOrganizaciones INNER JOIN
                      dbo.tb_Organizaciones ON dbo.tb_SubOrganizaciones.Org = dbo.tb_Organizaciones.idOrganizaciones AND 
                      dbo.tb_SubOrganizaciones.Org = dbo.tb_Organizaciones.idOrganizaciones LEFT OUTER JOIN
                      dbo.tb_Localidades ON dbo.tb_Localidades.idLocalidad = dbo.tb_SubOrganizaciones.Localidad
ORDER BY dbo.tb_SubOrganizaciones.Descripcion



*/
CREATE VIEW dbo.vwrSuborganizaciones
AS
/*
SELECT     TOP 100 PERCENT dbo.tb_SubOrganizaciones.idSubOrganizacion AS ID, '[' + CONVERT(VARCHAR, dbo.tb_Organizaciones.cuise) 
                      + '] ' + CASE WHEN dbo.tb_SubOrganizaciones.Baja = 'S' THEN '(Cerr.)-' ELSE ' ' END + RTRIM(LTRIM(SUBSTRING(dbo.tb_SubOrganizaciones.Descripcion,
                       1, 77))) + ' ' + CASE WHEN C.IdCategoria = 4 THEN C.Mnemo ELSE '' END + REPLICATE(' ', 
                      77 - LEN(RTRIM(LTRIM(SUBSTRING(dbo.tb_SubOrganizaciones.Descripcion, 1, 77))))) + '- ' + dbo.tb_Localidades.localidad AS Descripcion, 
                      dbo.tb_SubOrganizaciones.Org, dbo.tb_SubOrganizaciones.EsPropia, dbo.tb_SubOrganizaciones.Nivel, dbo.tb_SubOrganizaciones.Modalidad, 
                      dbo.tb_SubOrganizaciones.Zona, dbo.tb_SubOrganizaciones.Localidad, dbo.tb_SubOrganizaciones.Categoria
FROM         dbo.tb_SubOrganizaciones INNER JOIN
                      dbo.tb_Organizaciones ON dbo.tb_SubOrganizaciones.Org = dbo.tb_Organizaciones.idOrganizaciones AND 
                      dbo.tb_SubOrganizaciones.Org = dbo.tb_Organizaciones.idOrganizaciones LEFT OUTER JOIN
                      dbo.tb_Localidades ON dbo.tb_Localidades.idLocalidad = dbo.tb_SubOrganizaciones.Localidad LEFT OUTER JOIN
                      dbo.tb_CategoriasDeSuborganizaciones C ON C.IdCategoria = dbo.tb_SubOrganizaciones.Categoria
ORDER BY dbo.tb_SubOrganizaciones.Descripcion

*/


SELECT     TOP 100 PERCENT s.idSubOrganizacion AS ID, '[' + CONVERT(VARCHAR, o.cuise) 
                      + '] ' + CASE WHEN s.Baja = 'S' THEN '(Cerr.)-' ELSE ' ' END 
                      + RTRIM(LTRIM(SUBSTRING(s.Descripcion, 1, 77))) + ' ' 
                      + CASE WHEN C.IdCategoria = 4 THEN C.Mnemo ELSE '' END 
                      + CASE WHEN O.grupoorganizacion =1 AND S.nivel NOT IN (12,17) THEN '('+N.Mnemo  + ') ' ELSE '' end
                      + REPLICATE(' ', 77 - LEN(RTRIM(LTRIM(SUBSTRING(s.Descripcion, 1, 77))))) + '- ' + L.localidad AS Descripcion, 
                      s.Org, s.EsPropia, s.Nivel, s.Modalidad, 
                      s.Zona, s.Localidad, s.Categoria
FROM         dbo.tb_SubOrganizaciones S
INNER JOIN   dbo.tb_Organizaciones O ON s.Org = o.idOrganizaciones AND 
                      s.Org = o.idOrganizaciones 
LEFT OUTER JOIN  dbo.tb_Localidades L ON L.idLocalidad =S.Localidad 
LEFT OUTER JOIN  dbo.tb_CategoriasDeSuborganizaciones C ON C.IdCategoria = S.Categoria 
LEFT OUTER JOIN  tb_NivelesEnsenanza N ON N.idNivelEnsenanza = S.Nivel
ORDER BY S.Descripcion
GO
GRANT SELECT ON  [dbo].[vwrSuborganizaciones] TO [CertificacionesServicios]
GRANT SELECT ON  [dbo].[vwrSuborganizaciones] TO [Direcciones De Nivel]
GRANT SELECT ON  [dbo].[vwrSuborganizaciones] TO [Juntas]
GRANT SELECT ON  [dbo].[vwrSuborganizaciones] TO [public]
GRANT SELECT ON  [dbo].[vwrSuborganizaciones] TO [SoloVer]
GO
EXEC sp_addextendedproperty N'MS_DiagramPane1', N'[0E232FF0-B466-11cf-A24F-00AA00A3EFFF, 1.00]
Begin DesignProperties = 
   Begin PaneConfigurations = 
      Begin PaneConfiguration = 0
         NumPanes = 4
         Configuration = "(H (1[40] 4[20] 2[20] 3) )"
      End
      Begin PaneConfiguration = 1
         NumPanes = 3
         Configuration = "(H (1 [50] 4 [25] 3))"
      End
      Begin PaneConfiguration = 2
         NumPanes = 3
         Configuration = "(H (1[50] 2[25] 3) )"
      End
      Begin PaneConfiguration = 3
         NumPanes = 3
         Configuration = "(H (4 [30] 2 [40] 3))"
      End
      Begin PaneConfiguration = 4
         NumPanes = 2
         Configuration = "(H (1 [56] 3))"
      End
      Begin PaneConfiguration = 5
         NumPanes = 2
         Configuration = "(H (2 [66] 3))"
      End
      Begin PaneConfiguration = 6
         NumPanes = 2
         Configuration = "(H (4 [50] 3))"
      End
      Begin PaneConfiguration = 7
         NumPanes = 1
         Configuration = "(V (3))"
      End
      Begin PaneConfiguration = 8
         NumPanes = 3
         Configuration = "(H (1 [56] 4 [18] 2))"
      End
      Begin PaneConfiguration = 9
         NumPanes = 2
         Configuration = "(H (1 [75] 4))"
      End
      Begin PaneConfiguration = 10
         NumPanes = 2
         Configuration = "(H (1[66] 2) )"
      End
      Begin PaneConfiguration = 11
         NumPanes = 2
         Configuration = "(H (4 [60] 2))"
      End
      Begin PaneConfiguration = 12
         NumPanes = 1
         Configuration = "(H (1) )"
      End
      Begin PaneConfiguration = 13
         NumPanes = 1
         Configuration = "(V (4))"
      End
      Begin PaneConfiguration = 14
         NumPanes = 1
         Configuration = "(V (2))"
      End
      ActivePaneConfig = 0
   End
   Begin DiagramPane = 
      Begin Origin = 
         Top = 0
         Left = 0
      End
      Begin Tables = 
         Begin Table = "tb_SubOrganizaciones"
            Begin Extent = 
               Top = 6
               Left = 38
               Bottom = 322
               Right = 235
            End
            DisplayFlags = 280
            TopColumn = 4
         End
         Begin Table = "tb_Organizaciones"
            Begin Extent = 
               Top = 0
               Left = 509
               Bottom = 108
               Right = 698
            End
            DisplayFlags = 280
            TopColumn = 0
         End
         Begin Table = "tb_Localidades"
            Begin Extent = 
               Top = 121
               Left = 662
               Bottom = 229
               Right = 851
            End
            DisplayFlags = 280
            TopColumn = 0
         End
         Begin Table = "C"
            Begin Extent = 
               Top = 219
               Left = 452
               Bottom = 312
               Right = 641
            End
            DisplayFlags = 280
            TopColumn = 0
         End
      End
   End
   Begin SQLPane = 
   End
   Begin DataPane = 
      Begin ParameterDefaults = ""
      End
      RowHeights = 220
      Begin ColumnWidths = 10
         Width = 284
         Width = 1440
         Width = 10695
         Width = 1440
         Width = 1440
         Width = 1440
         Width = 1440
         Width = 1440
         Width = 1440
         Width = 1440
      End
   End
   Begin CriteriaPane = 
      Begin ColumnWidths = 11
         Column = 1440
         Alias = 900
         Table = 1170
         Output = 720
         Append = 1400
         NewValue = 1170
         SortType = 1350
         SortOrder = 1410
         GroupBy = 1350
         Filter = 1350', 'USER', N'dbo', 'VIEW', N'vwrSuborganizaciones', NULL, NULL
GO
EXEC sp_addextendedproperty N'MS_DiagramPane2', N'
         Or = 1350
         Or = 1350
         Or = 1350
      End
   End
End
', 'USER', N'dbo', 'VIEW', N'vwrSuborganizaciones', NULL, NULL
GO
DECLARE @xp int
SELECT @xp=2
EXEC sp_addextendedproperty N'MS_DiagramPaneCount', @xp, 'USER', N'dbo', 'VIEW', N'vwrSuborganizaciones', NULL, NULL
GO
